<?php

/**
 * Pager rendered by means of a template
 *
 */
class TemplatePager extends Pager
{
	/**
	 * Template object
	 *
	 * @var Template
	 */
	protected $template;

	/**
	 * Nominal number of elements on selector list
	 *
	 * @var int
	 */
	protected $selectorLength;

	/**
	 * Script responsible for changing current page
	 *
	 * The actual sense of this field is specific to the template implementation (e.g. it could be an URL pointing to a server-side script or a name of the JavaScript function)
	 *
	 * @var string
	 */
	protected $pageChangeScript;
	
	/**
	 * Aditional parameters to Change script method
	 * @var string
	 */
	protected $pageChangeScriptExtraParams;

	/**
	 * Constructs pager object
	 *
	 * @param Template $template
	 * @param int $totalPages
	 */
	public function __construct($template, $totalPages)
	{
		parent::__construct($totalPages);
		$this->template = $template;
	}

	/**
	 * Sets nominal number of elements on selector list
	 *
	 * @param int $selectorLength
	 */
	public function setSelectorLength($selectorLength)
	{
		$this->selectorLength = $selectorLength;
	}

	/**
	 * Sets script responsible for changing current page
	 * @param string $script
	 */
	public function setPageChangeScript($script)
	{
		$this->pageChangeScript = $script;
	}
	
	/**
	 * Sets script responsible for changing current page
	 * @param string $script
	 */
	public function setPageChangeScriptExtraParams($params)
	{
		$this->pageChangeScriptExtraParams = $params;
	}

	/**
	 * Renders pager
	 *
	 * @return string HTML code
	 */
	public function &render()
	{
		$this->template->setRef('extraParams', $this->pageChangeScriptExtraParams);
		$this->template->setRef('pageChangeScript', $this->pageChangeScript);
		$this->template->setRef('pageCount', $this->getPageCount());
		$this->template->setRef('currentPage', $this->getCurrentPage());
		$this->template->setRef('hasPrevPage', $this->hasPrevPage());
		$this->template->setRef('hasNextPage', $this->hasNextPage());
		$this->template->setRef('pageSelector', $this->getPageSelector($this->selectorLength));

		return $this->template->render();
	}
}
?>